<?php
namespace app\service\message\receive\instruction;

use app\service\Base;
use app\model\Command;
use app\model\Plug;

class Text extends Base {
    public function run() {
        $command = Command::where(['command_id' => $this->message['commandId']])->find();
        if(!$command) {
            $command = Command::create([
                'command_id' => $this->message['commandId'],
                'command_name' => $this->message['commandName'],
                'status' => 1
            ]);
        }
        switch($command['status']){
            case 0:
                return $this->responseText('该指令被禁用');
            case 1:
                return $this->responseText('该指令正在开发');
            case 2:
                $commandClass = $command['command_path'];
                if(!$commandClass || !class_exists($commandClass)){
                    $plugs = Plug::where(['command_id' => $this->message['commandId']])->select();
                    foreach($plugs as $plug){
                        $obj = new ($plug['class_path'])($this);
                        $res = $obj->run();
                        if($res === true){
                            return ;
                        }
                    }
                    return ;
                }
                $obj = new $commandClass($this->param);
                return $obj -> run();
                break;
            default:
                break;
        }
    }
}